<?php
/*
 * 文章模型
 * 用来定义与网站文章有关的数据库操作
 *
 */
class Article_model extends Model{

    //构造函数
    function __construct(){
        parent::Model();
        $this->load->database();  //加载数据库
    }


    //批量获取文章大分类信息
    function get_bigclass_info(){
		$this->db->order_by("id", "asc"); 
        $query=$this->db->get('dc_bigclass');
        $result= $query->result_array();
		return $result;
    }

	//根据bc_id获取大分类信息
    function get_onebigclass_info($id){
		$this->db->select('bc_name');
		$this->db->where('Id',$id);
        $query=$this->db->get('dc_bigclass');
        $result= $query->row_array();
		if( ($query->row_array())==null ){
            $result=array('bc_name'=>'');
        }
		return $result;
    }

	//批量获取文章二级分类信息
	function get_allsmallclass_info(){
		$this->db->order_by("id", "asc"); 
        $query=$this->db->get('dc_smallclass');
        $result= $query->result_array();
		return $result;
	}

	//根据bc_id获取文章二级分类信息
    function get_smallclass_info($bc_id){
		$this->db->where('bc_id', $bc_id);
		$this->db->order_by("id", "asc");
        $query=$this->db->get('dc_smallclass');
        $result= $query->result_array();
		return $result;
    }

	//根据sc_id获取文章二级分类信息
    function get_onesmallclass_info($id){
		$this->db->select('sc_name');
		$this->db->where('Id',$id);
        $query=$this->db->get('dc_smallclass');
        $result= $query->row_array();
		if( ($query->row_array())==null ){
            $result=array('sc_name'=>'');
        }
		return $result;
    }

//-----------------------------大类别删除操作---------------------------------//
	
	//根据bc_id批量删除类下所属文章
    function bc_del_article($bc_id){
		$this->db->where("bc_id",$bc_id); 
        $query=$this->db->delete('dc_article');
    }

	//根据bc_id批量删除二级分类及类下所属文章
    function bc_del_smallclass($bc_id){
		$this->db->where("bc_id",$bc_id); 
        $query=$this->db->delete('dc_smallclass');
		$this->bc_del_article($bc_id);
    }

    //删除文章大分类信息
    function del_bigclass_info($id){
		$this->db->where("id",$id); 
        $query=$this->db->delete('dc_bigclass');//删除大分类
		$this->bc_del_smallclass($id);//删除二级分类及下属文章
    }
//------------------------------------------------------------------------//

//-----------------------------二级类别删除操作-----------------------------//

	//根据sc_id批量删除类下所属文章
    function sc_del_article($sc_id){
		$this->db->where("sc_id",$sc_id); 
        $query=$this->db->delete('dc_article');
    }

	//根据sc_id批量删除二级分类及类下所属文章
    function sc_del_smallclass($sc_id){
		$this->db->where("id",$sc_id); 
        $query=$this->db->delete('dc_smallclass');//删除二级分类
		$this->sc_del_article($sc_id);//删除文章
    }

//---------------------------------------------------------------------//


//------------------------------增加分类操作------------------------------//

    //增加文章大分类信息
    function add_bigclass_info($bc_name){
		$this->db->set('bc_name', $bc_name);
        $query=$this->db->insert('dc_bigclass');
        $result= $this->db->insert_id();
		return $result;//返回刚插入的ID号
    }

    //增加文章二级分类信息
    function add_smallclass_info($bc_id,$sc_name){
		$data = array(
               'bc_id' => $bc_id,
               'sc_name' => $sc_name ,
            );
		$query=$this->db->insert('dc_smallclass',$data); 
        $result= $this->db->insert_id();
		return $result;//返回刚插入的ID号
    }

//---------------------------------------------------------------------//

//------------------------------编辑分类操作------------------------------//

    //编辑文章大分类信息
    function edit_bigclass_info($bc_id,$bc_name){
		$this->db->where('id',$bc_id);
		$data=array('bc_name'=>$bc_name);
		$query=$this->db->update('dc_bigclass',$data); 
		if($this->db->affected_rows()==1){
            return 0;//更新成功，返回0
        }else{
            return 1;//失败，返回1
        }
    }

	//编辑文章二级分类信息
    function edit_smallclass_info($sc_id,$sc_name){
		$this->db->where('Id',$sc_id);
		$data=array('sc_name'=>$sc_name);
		$query=$this->db->update('dc_smallclass',$data); 
		if($this->db->affected_rows()==1){
            return 0;//更新成功，返回0
        }else{
            return 1;//失败，返回1
        }
    }

	//根据sc_id修改文章大类
    function change_bigclass_article($bc_id,$sc_id){
		$this->db->where('sc_id',$sc_id);
		$data=array('bc_id'=>$bc_id);
		$query=$this->db->update('dc_article',$data); 
		if($this->db->affected_rows()==1){
            return 0;//更新成功，返回0
        }else{
            return 1;//失败，返回1
        }
    }

	//编辑文章二级分类所属大分类
	function change_bigclass($sc_id,$newbc_id) {
		$this->db->where('Id',$sc_id);
		$data=array('bc_id'=>$newbc_id);
		$query=$this->db->update('dc_smallclass',$data); 
		if($this->db->affected_rows()==1){
            return 0;//更新成功，返回0
        }else{
            return 1;//失败，返回1
        }
	}
	

//---------------------------------------------------------------------//

	//插入新文章
	function add_article($article) {
		$data = array(
            'areacode' => $article['areacode'] ,
            'bc_id' => $article['bigclass'] ,
			'sc_id' => $article['smallclass'] ,
			'title' => $article['title'] ,
			'summary' => $article['summary'] ,
			'content' => $article['content'] ,
			'image' => $article['image'] ,
			'tags' => $article['tags'] ,
			'author' => $article['author'] ,
			'puttime' => $article['puttime'] ,
			'about_hsid' => $article['hospital']);
		 $this->db->insert('dc_article', $data);    //插入数据
		 if(($this->db->affected_rows())!=1){
            return 1;      //如果插入操作影响的行数不为1，则表示插入失败，返回1
        }else{
            return 0;    //如果插入成功，返回0
        }
	}

	//获取所有文章
	function get_all_article() {
		$this->db->order_by('puttime','desc');
        $query=$this->db->get('dc_article');
        $result= $query->result_array();
		return $result;
	}


	//根据areacode获取文章条数
	function all_article_num($areacode='ALL') {
		if($areacode!='ALL') {
			$query_string="select count(*) as c from dc_article where areacode='".$areacode."'";
		}else{
			$query_string="select count(*) as c from dc_article";
		}
		$query = $this->db->query($query_string);
		$result= $query->result_array();
		return $result[0]['c'];
	}

	//根据地区获取所有文章列表
	function get_article_list($areacode='ALL',$per_nums,$start_position) {
		$this->db->limit($per_nums,$start_position);
		$this->db->order_by('Id','desc');
		if ($areacode!='ALL'){
			$this->db->where('areacode',$areacode);
		}
        $query=$this->db->get('dc_article');
        $result= $query->result_array();
		$c=count($result)-1;
		for($i=0;$i<=$c;$i++) {
			$bc_name=$this->get_onebigclass_info($result[$i]['bc_id']);
			$sc_name=$this->get_onesmallclass_info($result[$i]['sc_id']);
			$result[$i]['bc_id']=$bc_name['bc_name'];
			$result[$i]['sc_id']=$sc_name['sc_name'];
		}
		return $result;
	}

	//根据ID获取文章
	function get_one_article($id) {
		$this->db->where('Id',$id);
        $query=$this->db->get('dc_article');
        $result= $query->row_array();
		return $result;
	}

	//根据HSID获取文章(默认显示10条)
	function get_hos_article($hs_id,$title=0,$num=10) {
		$this->db->limit($num);
		$this->db->where('about_hsid',$hs_id);
		$this->db->order_by('Id','desc');
		if($title!=0) {	//如果只需要文章标题
			$this->db->select('Id, title, puttime');
		}
        $query=$this->db->get('dc_article');
        $result= $query->result_array();
		return $result;
	}

	//编辑文章
	function edit_article($id,$article) {
		$data = array(
            'bc_id' => $article['bigclass'] ,
			'sc_id' => $article['smallclass'] ,
			'title' => $article['title'] ,
			'summary' => $article['summary'] ,
			'content' => $article['content'] ,
			'tags' => $article['tags'] ,
			'author' => $article['author'] ,
			'puttime' => $article['puttime'] ,
			'about_hsid' => $article['hospital']);
		if ($article['image'] != '') {//如果不为空，则更新图片字段
			$data['image'] = $article['image'];
			}
		$this->db->where('id',$id);
		$query=$this->db->update('dc_article',$data); 
		if($this->db->affected_rows()==1){
            return 0;//更新成功，返回0
        }else{
            return 1;//失败，返回1
        }
	}

	//删除文章
	function del_article($id) {
		$this->db->where("Id",$id); 
        $query=$this->db->delete('dc_article');
	}

//--------------------------------------讲座信息部分---------------------------------------//
	//插入讲座信息
	function add_lecture($lec) {
		$data = array(
            'areacode' => $lec['areacode'] ,
            'lec_title' => $lec['lec_title'] ,
			'lec_time' => $lec['lec_time'] ,
			'lec_address' => $lec['lec_address'] ,
			'lec_image' => $lec['lec_image'] ,
			'lec_intro' => $lec['lec_intro'] ,
			'lec_tel' => $lec['lec_tel'] ,
			'puttime' => $lec['puttime'] );
		 $this->db->insert('dc_lecture', $data);    //插入数据
		 if(($this->db->affected_rows())!=1){
            return 1;      //如果插入操作影响的行数不为1，则表示插入失败，返回1
        }else{
            return 0;    //如果插入成功，返回0
        }
	}

	//获取讲座列表
	function get_all_lecture($areacode='ALL',$per_nums,$start_position) {
		$this->db->order_by('Id','desc');
		$this->db->limit($per_nums,$start_position);
		if( $areacode!='ALL' ) {
			$this->db->where('areacode',$areacode);
		}
        $query=$this->db->get('dc_lecture');
        $result= $query->result_array();
		return $result;
	}

	//获取单个讲座信息
	function get_one_lecture($id) {
		$this->db->where('Id',$id);
        $query=$this->db->get('dc_lecture');
        $result= $query->result_array();
		return $result;
	}

	//讲座条数
	function all_lecture_num($areacode='ALL') {
		if($areacode!='ALL') {
			$query_string="select count(*) as c from dc_lecture where areacode='".$areacode."'";
		}else{
			$query_string="select count(*) as c from dc_lecture";
		}
		$query = $this->db->query($query_string);
		$result= $query->result_array();
		return $result[0]['c'];
	}

	//编辑讲座信息
	function edit_lecture($id,$lec) {
		$data = array(
            'lec_title' => $lec['lec_title'] ,
			'lec_time' => $lec['lec_time'] ,
			'lec_address' => $lec['lec_address'] ,
			'lec_intro' => $lec['lec_intro'] ,
			'lec_tel' => $lec['lec_tel'] ,
			'puttime' => $lec['puttime'] );
		if ($lec['lec_image'] != '') {//如果不为空，则更新图片字段
			$data['lec_image'] = $lec['lec_image'];
			}
		$this->db->where('id',$id);
		 $this->db->update('dc_lecture', $data);
		 if(($this->db->affected_rows())!=1){
            return 1;      //如果操作影响的行数不为1，则表示失败，返回1
        }else{
            return 0;    //如果成功，返回0
        }
	}

	//删除讲座信息
	function del_lecture($id) {
		$this->db->where("Id",$id); 
        $query=$this->db->delete('dc_lecture');
	}

//------------------------------------首页应用部分---------------------------------------//

	//前台输出文章列表
	function output_article_list($areacode='ALL',$bc_id,$sc_id,$num) {
		$this->db->where('bc_id', $bc_id);
		if($sc_id!=0) {
			$this->db->where('sc_id', $sc_id);
		}
		if ($areacode != 'ALL') {
			$this->db->where('areacode', $areacode);
		}
		$this->db->order_by("Id", "desc"); 
		$this->db->limit($num);
		$query = $this->db->get('dc_article');
		$result= $query->result_array();
		return $result;
	}

	//二级页面获取某类别下的文章列表
	function list_article_get_list($areacode='ALL',$bc_id,$sc_id,$per_nums,$start_position) {
		if( $areacode!='ALL' ) {
			$this->db->where('areacode',$areacode);
		}
		$this->db->order_by('Id','desc');
		$this->db->where('bc_id',$bc_id);
		if($sc_id!=0) {
			$this->db->where('sc_id', $sc_id);
		}
		$this->db->limit($per_nums,$start_position);
		$this->db->select('Id, areacode, bc_id, sc_id, title, summary, image, puttime,hits,hitup');
		$query=$this->db->get('dc_article');
        $result= $query->result_array();
		return $result;
	}

	//获取某类别文章总条数
	function list_article_get_num($areacode='ALL',$bc_id,$sc_id) {
		if($areacode!='ALL') {
			$query_string="select count(*) as c from dc_article where areacode='".$areacode."' and bc_id='".$bc_id."' and sc_id='".$sc_id."' ";
		}else{
			$query_string="select count(*) as c from dc_article where bc_id='".$bc_id."' and sc_id='".$sc_id."' ";
		}
		$query = $this->db->query($query_string);
		$result= $query->result_array();
		return $result[0]['c'];
	}

	//输出热门文章排行
	function list_article_get_hot($areacode='ALL',$bc_id,$sc_id,$num) {
		$this->db->order_by('hits desc, hitup desc'); //根据点击量及好评数倒序排列
		$this->db->where('bc_id', $bc_id);
		if($sc_id!=0) {
			$this->db->where('sc_id', $sc_id);
		}
		if ($areacode != 'ALL') {
			$this->db->where('areacode', $areacode);
		}
		$this->db->order_by("Id", "desc"); 
		$this->db->limit($num);
		$this->db->select('Id, areacode, bc_id, sc_id, title, image, puttime,hits,hitup');
		$query = $this->db->get('dc_article');
		$result= $query->result_array();
		return $result;
	}

	//文章评价
	function hit($id,$action) {
		if($action=='up') {
			$this->db->set('hitup','hitup+1', FALSE);
		}else{
			$this->db->set('hitdown','hitdown+1', FALSE);
		}
		$this->db->where('Id',$id);
		$query=$this->db->update('dc_article');
		$result=$this->get_hit($id,$action);
		return $result;
	}

	function get_hit($id,$action) {
		$this->db->where('Id',$id);
		if($action=='up') {
			$this->db->select('hitup');
		}else{
			$this->db->select('hitdown');
		}
		$query=$this->db->get('dc_article');
		$result=$query->row_array();
		if(empty($result)) {
			return 0;//如果没有查到结果，返回0
		}
		if($action='up') {
			return $result['hitup'];
		}else{
			return $result['hitdown'];
		}
	}

	//点击量加一
	function add_hits($id) {
		$this->db->set('hits','hits+1', FALSE);
		$this->db->where('Id',$id);
		$query=$this->db->update('dc_article');
	}

}
?>
